package com.topboya.linker.ui.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.response.ProductInforListReponse;
import com.topboya.linker.ui.activity.SeeSelectedFileActivity;
import com.topboya.linker.ui.activity.SeeSelectedProductActivity;
import com.topboya.linker.ui.activity.SellectProductInforActivity;
import com.topboya.linker.ui.activity.xSelectFileActivity;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;

public class SeeSelectProductAdapter extends RecyclerView.Adapter<SeeSelectProductAdapter.ListItemViewHolder> {

    private static final String TAG = "SelectProductAdapter:";
    private SeeSelectedProductActivity mContext;
    private ArrayList<ProductInforListReponse.ProductDetailInfoList> mList;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private boolean mIsSelectable = false;


    public SeeSelectProductAdapter(SeeSelectedProductActivity mContext, ArrayList<ProductInforListReponse.ProductDetailInfoList> list) {

        if (list == null) {
            throw new IllegalArgumentException("model Data must not be null");
        }
        this.mContext = mContext;
        mList = list;
    }

    //更新adpter的数据和选择状态
    public void updateDataSet(ArrayList<ProductInforListReponse.ProductDetailInfoList> list) {
        this.mList = list;
        mSelectedPositions = new SparseBooleanArray();

    }


    //获得选中条目的结果
    public ArrayList<ProductInforListReponse.ProductDetailInfoList> getSelectedItem() {

        ArrayList<ProductInforListReponse.ProductDetailInfoList> selectList = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            if (isItemChecked(i)) {

                selectList.add(mList.get(i));
            }
        }
        return selectList;
    }

    public ArrayList<ProductInforListReponse.ProductDetailInfoList> selectAllorunSelectAll(boolean isselected){
//        将所有的条目变为选中状态
        ArrayList<ProductInforListReponse.ProductDetailInfoList> selectList = new ArrayList<>();
        if (isselected){
            for (int i = 0; i < mList.size(); i++) {

                selectList.add(mList.get(i));
                setItemChecked(i, true);
            }
        }else {

            for (int i = 0; i < mList.size(); i++) {

                selectList.add(mList.get(i));
                setItemChecked(i, false);
            }
        }
        notifyDataSetChanged();
        return selectList;


    }
    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.select_product_item, viewGroup, false);
        return new SeeSelectProductAdapter.ListItemViewHolder(itemView);
    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {

        mSelectedPositions.put(position, isChecked);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {

        return mSelectedPositions.get(position);
    }

    //根据位置判断条目是否可选
    private boolean isSelectable() {

        return mIsSelectable;
    }
    //设置给定位置条目的可选与否的状态
    private void setSelectable(boolean selectable) {

        mIsSelectable = selectable;
    }


    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final ListItemViewHolder holder, final int i) {

        //设置条目状态
        holder.tv_productName.setText(mList.get(i).getProductName());
        int isvisible =Integer.parseInt(mList.get(i).getFileNum()) > 0 ? View.VISIBLE : View.GONE;
        holder.img_data.setVisibility(isvisible);
        holder.tv_productcompany.setText(mList.get(i).getProductFactory());
        if (null !=mList.get(i).getFileLists() && mList.get(i).getFileLists().size() > 0){
            holder.tv_select_file_num.setVisibility(View.VISIBLE);
            holder.tv_select_file_num.setText(String.valueOf(mList.get(i).getFileLists().size()));
        }else {
            holder.tv_select_file_num.setVisibility(View.INVISIBLE);
        }
        if (isItemChecked(i)){

            holder.tv_productName.setTextColor(mContext.getResources().getColor(R.color.home_bottom_text_checked));

        }else {
            holder.tv_productName.setTextColor(Color.BLACK);
        }

        //条目view的监听
        holder.itemView.setOnClickListener(v -> {
            if (isItemChecked(i)) {

                setItemChecked(i, false);
                holder.tv_productName.setTextColor(Color.BLACK);

            } else {

                setItemChecked(i, true);
                holder.tv_productName.setTextColor(mContext.getResources().getColor(R.color.home_bottom_text_checked));

            }
//           notifyItemChanged(i);
            int count = getSelectedItem().size();
            itemClickListener.onClick(holder.itemView ,i,count);

        });
        holder.img_data.setOnClickListener(v1 -> {


            setItemChecked(i, true);
            holder.tv_productName.setTextColor(mContext.getResources().getColor(R.color.home_bottom_text_checked));
//           notifyItemChanged(i);
            int count = getSelectedItem().size();
            itemClickListener.onClick(holder.itemView ,i,count);
            ArrayList<ProductInforListReponse.ProductDetailInfoList> productDetailInfoLists = getSelectedItem();

            Intent intent = new Intent(mContext,xSelectFileActivity.class);
            intent.putExtra("productId",mList.get(i).getId());
            intent.putExtra("productDetailInfoLists",productDetailInfoLists);
            LogUtils.v(TAG + "productId:"+mList.get(i).getId());
            mContext.startActivityForResult(intent,1000);

        });

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_productName;
        TextView tv_productcompany;
        TextView tv_select_file_num;
        ImageView img_data;

        ListItemViewHolder(View view) {
            super(view);
            this.tv_productName = view.findViewById(R.id.tv_productName);
            this.tv_productcompany = view.findViewById(R.id.tv_productcompany);
            this.tv_select_file_num = view.findViewById(R.id.tv_select_file_num);
            this.img_data = view.findViewById(R.id.img_data);
        }
    }

    private SeeSelectProductAdapter.OnItemClickListener itemClickListener;

    public void setOnItemClickListener(SeeSelectProductAdapter.OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

    public interface  OnItemClickListener{

        void onClick(View v, int position, int count);
    }



}
